package com.codingvip.main;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.codingvip.entity.OrderEntity;
import com.codingvip.entity.UserEntity;
import com.codingvip.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 */
public class SampleApplication {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-database.xml");

        UserService service = ctx.getBean(UserService.class);
        // test begin ====>

        for (int i = 1; i < 2; i++) {
            OrderEntity order = service.getOrder(i);
            if (order != null) {
                System.out.println(JSON.toJSON(order));
            } else {
                order = new OrderEntity("NO-" + i);
                order.setOrderId(i);
                service.addOrder(order);
            }
        }

        for (int i = 1; i < 5; i++) {
            UserEntity user = service.getUser(i);
            if (user != null) {
                System.out.println(JSON.toJSON(user));
            } else {
                user = new UserEntity(i, "User-" + i);
                service.addUser(user);
            }
        }


        // <===== test end


        ctx.close();
    }
}
